//
//  MineInfoNewTwoCollectionViewCell.m
//  MJApp
//
//  Created by wangliang on 15/7/22.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoNewTwoCollectionViewCell.h"
#import "MineInfoNewTwoTableViewCell.h"
#import "MineInfoNewOneTableViewCell.h"

@interface MineInfoNewTwoCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>
{
    CGFloat _cellHigh;
    NSMutableArray *_nameArray;
    NSMutableArray *_descArray;
}

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation MineInfoNewTwoCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self UI];
        [self prepareData];
    }
    return self;
}

- (void)UI {
    
    
    _cellHigh = CGRectGetHeight(self.frame) / 7.5;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.separatorColor = [UIColor clearColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self addSubview:tableView];
    self.tableView = tableView;
    
    self.layer.cornerRadius = 5;
    self.layer.borderColor = [UIColor grayColor].CGColor;
    self.layer.borderWidth = 0.5;
}


- (void)prepareData {
    
    _nameArray = [NSMutableArray array];
    [_nameArray addObject:@"生日"];
    [_nameArray addObject:@"邮箱"];
    [_nameArray addObject:@"QQ"];
    [_nameArray addObject:@"所在地"];
    [_nameArray addObject:@"个人签名"];
    [_nameArray addObject:@"查看我的动态"];
    _descArray = [NSMutableArray array];
}


- (void)setInfoDic:(NSDictionary *)infoDic {
    
    if (infoDic == nil) {
        
        return;
    }
    _infoDic = infoDic;
    [_descArray removeAllObjects];
    [_descArray addObject:infoDic[@"birthday"]];
    [_descArray addObject:infoDic[@"email"]];
    [_descArray addObject:infoDic[@"qq"]];
    [_descArray addObject:infoDic[@"address"]];
    [_descArray addObject:infoDic[@"remark"]];
    [_descArray addObject:@""];
    if ([infoDic[@"id"] isEqual:[userInfoTool share].ID]) {
        
        _nameArray[_nameArray.count - 1] = @"查看我的动态";
    }else {
        _nameArray[_nameArray.count - 1] = @"查看TA的动态";
    }
    [self.tableView reloadData];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        return 1.5 * _cellHigh;
    }else {
        return _cellHigh;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellID = @"MineInfoNewTwoTableViewCell";
    if (indexPath.row == 0) {
        
        cellID = @"MineInfoNewOneTableViewCell";
    }
    UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        
        if (indexPath.row == 0) {
            
            cell = WL_XIB(@"MineInfoNewOneTableViewCell");
        }else {
            cell = WL_XIB(@"MineInfoNewTwoTableViewCell");
        }
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //数据
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    BOOL isShow = (indexPath.row == 6 ? YES : NO);
    dic[@"hidden"] = @(isShow);
    if (indexPath.row > 0) {
        
        dic[@"name"] = _nameArray[indexPath.row - 1];
        if (_descArray.count > 0) {
            
            dic[@"desc"] = _descArray[indexPath.row - 1];
        }
    }else {
        
        if (self.infoDic != nil) {
            
            dic[@"name"] = self.infoDic[@"nick_name"];
            NSString *sexName = [self.infoDic[@"sex"] integerValue] == 1 ? @"男" : @"女";
            dic[@"desc"] = [NSString stringWithFormat:@"%@,%@",self.infoDic[@"age"],sexName];
        }
    }
    
    
    if (indexPath.row != 0) {
        
        MineInfoNewTwoTableViewCell *theCell = (MineInfoNewTwoTableViewCell *)cell;
        theCell.infoDic = dic;
    }else {
        MineInfoNewOneTableViewCell *theCell = (MineInfoNewOneTableViewCell *)cell;
        theCell.infoDic = dic;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 6) {
        
        if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
            
            [self.delegate homeCell:self withInfo:self.infoDic];
        }
    }
}

@end
